package com.yumeng.common.helper;

import com.yumeng.common.data.constants.DateConstants;
import com.yumeng.common.data.enums.DateMode;
import com.yumeng.common.exception.YumengException;
import com.yumeng.common.helper.bean.KssjJssjBo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author wxd
 * @date 2022/9/1 17:00
 */
@Slf4j
public class LocalDateExtHelper {

    /**
     * 获取时间列表
     * @param dataFlag
     * @param colCount
     * @param mode
     * @return
     */
    public static List<String> getColNameList(LocalDate dataFlag, Integer colCount, DateMode mode){
        String showFormat;
        int maxCol = 12;
        if (DateMode.DAY.equals(mode)){
            showFormat = DateConstants.YYYY_MM_DD;//"yyyy-MM-dd"
            maxCol = 31;
        }else if (DateMode.MONTH.equals(mode)){
            showFormat = DateConstants.YYYY_MM;//"yyyy-MM"
        }else if (DateMode.YEAR.equals(mode)){
            showFormat = DateConstants.YYYY;//"yyyy"
        }else {
            throw new YumengException("日期模式异常");
        }
        //设置默认列数
        if (colCount == null || colCount == 0){
            if (DateMode.DAY.equals(mode)){
                colCount = 8;
            }else if(DateMode.MONTH.equals(mode)){
                colCount = 6;
            }else if(DateMode.YEAR.equals(mode)){
                colCount = 4;
            }
        }
        colCount = colCount > maxCol ? maxCol : colCount;
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(showFormat);
        List<String> list = new ArrayList<>();
        for (int i = colCount-1; i >= 0; i--){
            if (DateMode.DAY.equals(mode)){
                list.add(dataFlag.minusDays(i).format(fmt));
            }else if(DateMode.MONTH.equals(mode)){
                list.add(dataFlag.minusMonths(i).format(fmt));
            }else if(DateMode.YEAR.equals(mode)){
                list.add(dataFlag.minusYears(i).format(fmt));
            }
        }
        return list;
    }

    /**
     * 获取时间列表
     * @param kssj
     * @param jssj
     * @param colCount
     * @param mode
     * @return
     */
    public static List<String> getColNameList(String kssj, String jssj, Integer colCount, DateMode mode){
        if (StringUtils.isBlank(kssj) || StringUtils.isBlank(jssj)){
            return new ArrayList<>();
        }

        DateTimeFormatter defFmt = DateTimeFormatter.ofPattern(DateConstants.YYYY_MM_DD);
        LocalDate ksDate = LocalDate.parse(kssj.substring(0, 10), defFmt);
        LocalDate jsDate = LocalDate.parse(jssj.substring(0, 10), defFmt);
        if (ksDate.compareTo(jsDate) > 0){
            throw new YumengException("开始时间大于结束时间");
        }

        String showFormat;
        int maxCol = 12;
        if (DateMode.DAY.equals(mode)){
            showFormat = DateConstants.YYYY_MM_DD;//"yyyy-MM-dd"
            maxCol = 31;
        }else if (DateMode.MONTH.equals(mode)){
            showFormat = DateConstants.YYYY_MM;//"yyyy-MM"
        }else if (DateMode.YEAR.equals(mode)){
            showFormat = DateConstants.YYYY;//"yyyy"
        }else {
            throw new YumengException("日期模式异常");
        }
        //设置默认列数
        if (colCount == null || colCount == 0){
            if (DateMode.DAY.equals(mode)){
                colCount = 8;
            }else if(DateMode.MONTH.equals(mode)){
                colCount = 6;
            }else if(DateMode.YEAR.equals(mode)){
                colCount = 4;
            }
        }
        colCount = colCount > maxCol ? maxCol : colCount;
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(showFormat);
        String ksDateStr = ksDate.format(fmt); //2021 2022
        String tempDateStr = jsDate.format(fmt);//2022
        List<String> list = new ArrayList<>();
        int index = 0;
        while (ksDateStr.compareTo(tempDateStr) <= 0){
            list.add(tempDateStr);
            index++;
            if (index >= colCount){
                break;
            }

            LocalDate tempDate = null;
            if (DateMode.DAY.equals(mode)){
                tempDate = jsDate.minusDays(index);
            }else if(DateMode.MONTH.equals(mode)){
                tempDate = jsDate.minusMonths(index);
            }else if(DateMode.YEAR.equals(mode)){
                tempDate = jsDate.minusYears(index);
            }
            tempDateStr = tempDate.format(fmt);
        }
        Collections.sort(list);
        return list;
    }

    public static KssjJssjBo getYearKssjJssj(){
        return getYearKssjJssj(LocalDateHelper.getCurrYear());
    }
    /**
     * 获取指定年份的开始、结束时刻
     * @param year
     * @return
     */
    public static KssjJssjBo getYearKssjJssj(int year){
        String kssk = LocalDateHelper.getStartTime(year, 1);
        String jssj = LocalDateHelper.getEndTime(year, 12);
        return new KssjJssjBo(kssk, jssj);
    }

    public static KssjJssjBo getMonthKssjJssj(){
        return getMonthKssjJssj(LocalDateHelper.getCurrYear(), LocalDateHelper.getCurrMonth());
    }
    /**
     * 获取指定年份、月份的开始、结束时刻
     * @param year
     * @param month
     * @return
     */
    public static KssjJssjBo getMonthKssjJssj(int year, int month){
        String kssk = LocalDateHelper.getStartTime(year, month);
        String jssj = LocalDateHelper.getEndTime(year, month);
        return new KssjJssjBo(kssk, jssj);
    }

    /**
     * 获取指定年份、季度的开始、结束时刻
     * @param year
     * @param quarter
     * @return
     */
    public static KssjJssjBo getQuarterKssjJssj(int year, int quarter){
        int ksMonth = 0;
        int jsMonth = 0;
        if (quarter == 1){
            ksMonth = 1;
            jsMonth = 3;
        }else if (quarter == 2){
            ksMonth = 4;
            jsMonth = 6;
        }else if (quarter == 3){
            ksMonth = 7;
            jsMonth = 9;
        }else if (quarter == 4){
            ksMonth = 10;
            jsMonth = 12;
        }else{
            throw new YumengException("季度值超出范围");
        }
        String kssk = LocalDateHelper.getStartTime(year, ksMonth);
        String jssj = LocalDateHelper.getEndTime(year, jsMonth);
        return new KssjJssjBo(kssk, jssj);
    }

    public static void main(String[] args){
/*        List<String> s1 = getColNameList(LocalDate.now(), 30, StatConstants.TIME_MODE_YEAR);
        log.info("{}-{}", s1.size(), JSONObject.toJSONString(s1));

        String kssj = "2008-01-01 00:00:00";
        String jssj = "2022-12-01 23:59:59";
        List<String> s2 = getColNameList(kssj, jssj, 31, StatConstants.TIME_MODE_MONTH);
        log.info("{}-{}", s2.size(), JSONObject.toJSONString(s2));*/
    }

}
